package com.ipfsemc.ipfsemc.controller.v1;

import com.ipfsemc.ipfsemc.model.Product;
import com.ipfsemc.ipfsemc.service.IProductService;
import com.ipfsemc.ipfsemc.swagger.ApiModelPropertyIgnore;
import com.ipfsemc.ipfsemc.util.JsonData;
import com.ipfsemc.ipfsemc.util.RespList;
import com.ipfsemc.ipfsemc.util.Response;
import com.ipfsemc.ipfsemc.util.ResponseBase;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("product")
@Api(value = "ProductController", tags = "产品")
public class ProductController {
    private static final Logger logger = LoggerFactory.getLogger(ProductController.class);
    @Autowired
    private IProductService productService;

    @ApiOperation(value = "首页查询商品",httpMethod = "GET")
    @RequestMapping(value = "listProduct",method = RequestMethod.GET)
    public RespList<Product> listProduct() {
        RespList jsonData = new RespList();
        try {
            List<Product> products = productService.listProduct();
            jsonData.setResult(products);
            jsonData.setCode(0);

        } catch (Exception ex) {
            logger.error("[product][list]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    @ApiOperation(value = "后台查询所有商品",httpMethod = "GET")
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public RespList<Product> list(Product product) {
        RespList jsonData = new RespList();
        try {
            List<Product> products = productService.list(product);
            jsonData.setResult(products);
            jsonData.setCode(0);

        } catch (Exception ex) {
            logger.error("[product][list]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    @ApiOperation(value = "添加/修改商品",httpMethod = "POST")
    @PostMapping("editProduct")
    public ResponseBase addProduct(
            @ApiModelPropertyIgnore("proId")
             Product product){
        ResponseBase jsonData=new ResponseBase();
        try {
            int edit=-1 ;
            if (product.getProId()==null||product.getProId().isEmpty()){
                edit = productService.add(product);
            }else {
                edit = productService.update(product);
            }
            if(edit<0){
                jsonData.setMessage("操作失败！！");
            }else {
                jsonData.setMessage("操作成功！！");

                jsonData.setCode(0);
            }
        } catch (Exception ex) {
            logger.error("[product][addProduct]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    //注意：待解决！！！！   商品删除了  但是类别却没有删   解决方式  利用联表删  或者另外写一个方法
    @ApiOperation(value = "删除商品",httpMethod = "POST")
    @PostMapping("delProduct")
    @ApiImplicitParams({
            @ApiImplicitParam(name="proId",value="产品ID",required=true,dataType="String"),
    })
    public ResponseBase delProduct(String proId){
        ResponseBase jsonData=new ResponseBase();
        try {

            int del = productService.del(proId);
            if(del<0){
                jsonData.setMessage("操作失败！！");
            }else {
                jsonData.setMessage("操作成功！！");

                jsonData.setCode(0);
            }
        } catch (Exception ex) {
            logger.error("[product][delProduct]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    @ApiOperation(value = "商品详情",httpMethod = "GET")
    @RequestMapping(value = "selectProByKey",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="proId",value="产品ID",required=true,dataType="String")
    })
    public Response<Product> selectProByKey(String proId) {

        Response jsonData = new Response();
        try {
            if (null == proId) {
                jsonData.setMessage("参数有误");
            }else{
                Product pros = productService.selectProByKey(proId);
                jsonData.setResult(pros);
                jsonData.setCode(0);
            }

        } catch (Exception ex) {
            logger.error("[product][selectProByCategory]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }

        return jsonData;
    }


}
